package main.java.org.jmtp.sync.model;

import java.util.List;

import main.java.org.jmtp.sync.util.HibernateUtil;

import org.hibernate.Session;

public class TrackManager {
	
	public TrackManager () {
		
	}
	
	/**
	 * Retreive all tracks in database
	 * @return A list of tracks
	 */
	public List<Track> getAllTracks() {
		return getTracksFromQuery("from TRACKS");
	}
	/**
	 * Retrieve a list of tracks with the query given in parameter
	 * @param query The query to execute
	 * @return A list of Tracks
	 */
	public List<Track> getTracksFromQuery(String query) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		List<Track> result = (List<Track>) session.createQuery(query).list();
		
		session.getTransaction().commit();
		
		return result;
	}
	/**
	 * Save a track in database
	 * @param t Track to save in database
	 */
	public void saveInDb (Track t) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.save(t);
		
		session.getTransaction().commit();
	}
}
